package com.miyake.dsp.osa.lab.wrapper;

import java.util.LinkedList;
import java.util.List;

import com.miyake.dsp.ComplexArray;
import com.miyake.dsp.osa.lab.Calculator;
import com.miyake.dsp.osa.lab.ExtractSetting;
import com.miyake.dsp.osa.lab.LabUiAnnotation;
@LabUiAnnotation(caption="Extract by Y")
public class ExtractYWrapper extends Calculator {
	public static final String INPUT_X = "In-X";
	public static final String INPUT_Y = "In-Y";
	public static final String OUTPUT_X = "Out-X";
	public static final String OUTPUT_Y = "Out-Y";
	public static final String OUT_STARTINDEX = "Index Start";
	public static final String OUT_ENDINDEX = "Index End";
	private ExtractSetting setting;
	
	public ExtractYWrapper(ExtractSetting setting) {
		this.setting = setting;
		initInput(INPUT_X);
		initInput(INPUT_Y);
		initOutput(OUTPUT_X);
		initOutput(OUTPUT_Y);
		initOutput(OUT_STARTINDEX);
		initOutput(OUT_ENDINDEX);
	}
	
	@Override
	public synchronized void process() {
		double[] y = getInput(INPUT_Y).getRealPart();
		double[] x = getInput(INPUT_X).getRealPart();
		List<Integer> list = new LinkedList<Integer>();
		for (int i = 0; i < y.length; i++) {
			if (y[i] > setting.getMin() && y[i] < setting.getMax()) {
				list.add(i);
			}
		}
		double[] xout = new double[list.size()];
		double[] yout = new double[list.size()];
		int i = 0;
		for (int index : list) {
			xout[i] = x[index];
			yout[i] = y[index];
			i++;
		}
		setOutput(OUTPUT_X, new ComplexArray(xout));
		setOutput(OUTPUT_Y, new ComplexArray(yout));
		
		double[] outStartIndex = new double[1];
		double[] outEndIndex = new double[1];
		outStartIndex[0] = list.get(0).doubleValue();
		outEndIndex[0] = list.get(list.size()-1).doubleValue();
		setOutput(OUT_STARTINDEX, new ComplexArray(outStartIndex));
		setOutput(OUT_ENDINDEX, new ComplexArray(outEndIndex));
	}

	@Override
	public Object getSetting() {
		return this.setting;
	}

}
